﻿using DAL.comm;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class CategoryDAL
    {
        private SqlCommand cmd;//新建SqlCommand对象
        public CategoryDAL()
        {
            cmd = new SqlCommand();//实例化sqlcommand对象
            cmd.CommandType = CommandType.StoredProcedure;//定义sqlmmand对象访问数据库方式为存储过程
        }/// <summary>
        /// 针对数据库修改提出公共实现方法
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        public bool ForEdit(SqlCommand cmd)
        {
            try
            {
                int num = SQLHelper.ExecuteSQL(cmd);
                if (num > 0)
                    return true;
                else
                    return false;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }/// <summary>
        /// 加载存储过程参数方法实现
        /// </summary>
        /// <param name="bo"></param>

        private void SetPara(Category bo)
        {
            cmd.Parameters.Add("@CategoryID", SqlDbType.NVarChar, 6).Value = bo.CategoryID;
            cmd.Parameters.Add("@CategoryName", SqlDbType.NVarChar, 80).Value = bo.CategoryName;
        }/// <summary>
        /// 新增数据方法实现
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public bool Insert(Category bo)
        {
            cmd.CommandText = "InsertCategory";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }/// <summary>
        /// 数据修改方法
        /// </summary>
        /// <param name="bo"></param>
        /// <returns></returns>
        public bool Update(Category bo)
        {
            cmd.CommandText = "UpdateCategory";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }/// <summary>
         /// 数据删除方法
         /// </summary>
         /// <param name="id"></param>
         /// <returns></returns>
        public bool Delete(Category bo)
        {
            cmd.CommandText = "DeleteCategory";
            cmd.Parameters.Clear();
            SetPara(bo);
            return ForEdit(cmd);
        }
        /// <summary>
        /// 根据传入的ID查找对应记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Category  FindByID(string id)
        {
            cmd.CommandText = "FindCategoryByID";
            cmd.Parameters.Clear();
            cmd.Parameters.Add("@CategoryID", SqlDbType.NVarChar, 6).Value = id;
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            Category bo = new Category();
            while (read.Read())
            {
                bo.CategoryID = read["CategoryID"].ToString().Trim();//将当前循环的记录列名为categoryID的数据值赋值给bo.categoryID
                bo.CategoryName = read["CategoryName"].ToString().Trim();
            }
            read.Close();//关闭读库
            return bo;
        }
       

        public List<Category> GetList()
        {
            cmd.CommandText = "SelectCategoryList";//指定存储过程名称
            cmd.Parameters.Clear();//清空存储过程参数列表
            List<Category> list = new List<Category>();//定义泛型集合用于存储数据库返回数据
            SqlDataReader read = SQLHelper.ExcuteReader(cmd);//调用通用数据访问类进行读库操作
            while (read.Read())
            {
                Category bo = new Category();//实例化Category类，用于存储一条产品类型记录
                bo.CategoryID = read["CategoryID"].ToString().Trim();//将当前循环的记录列名为CategoryID的数据值赋值给bo.Category
                bo.CategoryName = read["CategoryName"].ToString().Trim();
                list.Add(bo);//将当前记录添加至泛型集合list中
            }
            read.Close();//关闭读库
            return list;
        }
    }
}
